var frndViewList = new Array();
var newToneViewIndx = 0;
var dlViewIndx = 0;
var frndViewIndx = 0;

function showPostInstallFrame(contentId) {
	var singleUrl = "http://offers.thumbplay.com/plp/social/landing.do?method=home&pname=1456&ptrx=social&offerType=1xpOnly";
	var bonusUrl = "http://offers.thumbplay.com/plp/social/landing.do?method=home&pname=1456&ptrx=social";
	
	var frm = document.getElementById('tppurchaseflow');
	
	//if(globalPurchaseData.length > 0){
		frm.src = bonusUrl+"&thpcampid="+viewer.getId()+"&onexpOffer=true&selectedContent="+contentId;
	/*} else {
		frm.src = singleUrl+"&thpcampid="+viewer.getId()+"&onexpOffer=true&selectedContent="+contentId;
	}*/
	
	frm.style.display = "";
	document.getElementById('loading').style.display = "none";
	document.getElementById('gallery_module').style.display = "none";
	document.getElementById('purchase_module').style.display = "";
	
	gadgets.window.adjustHeight();
}

function loadFriends(){
	var req = opensocial.newDataRequest();
    req.add(req.newFetchPersonRequest(opensocial.IdSpec.PersonId.VIEWER), 'viewer');
  
    var viewerFriends = opensocial.newIdSpec({ "userId" : "VIEWER", "groupId" : "FRIENDS" });
    var opt_params = {};
    opt_params[opensocial.DataRequest.PeopleRequestFields.MAX] = 100;
    opt_params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = [opensocial.Person.Field.MUSIC];
    req.add(req.newFetchPeopleRequest(viewerFriends, opt_params), 'viewerFriends');
    req.add(req.newFetchPersonRequest("OWNER"), 'owner');
    
    req.send(loadFriendsHandler);
}

function loadFriendsHandler(data) {
	var viewer = data.get('viewer').getData();
	var friends = data.get('viewerFriends').getData();
	owner = data.get('owner').getData();
	
	friends.each(function(p){
		if(p.getId()){
			//html += "<img src='"+p.getField(opensocial.Person.Field.THUMBNAIL_URL)+"'/>" + "<br/>" + p.getDisplayName() + " likes " + p.getField(opensocial.Person.Field.MUSIC)+"<br/>";
			
			if(typeof p.getField(opensocial.Person.Field.MUSIC) != "undefined"){
				var friend = new Object();
				friend.friendId = p.getId();
				friend.friendName = p.getDisplayName();
				friend.thumbnail = p.getField(opensocial.Person.Field.THUMBNAIL_URL);
				friend.favMusic = new Array();
				friend.favMusic = p.getField(opensocial.Person.Field.MUSIC).toString().split(new RegExp("[,\\n;]"));
				friendData.push(friend);
			} else {
				var friend = new Object();
				friend.friendId = p.getId();
				friend.friendName = p.getDisplayName();
				friendData.push(friend);
			}
		}
	}			
	);
	
	loadFriendMusic();
}

function loadFriendMusic(){
	for(var i=0; i<friendData.length; i++){
		var f = friendData[i];
		if((typeof f.favMusic != 'undefined') && f.favMusic.length > 0){
			for(var j=0; j<f.favMusic.length && j<4; j++){
				if(artistData.length > 0){
					for(var k=0; k<artistData.length; k++){
						var artst = artistData[k];
						if(artst.artist != f.favMusic[j]){
							artistCount++;
						} 
					}
				} else {
					artistCount++;
				}
			}
		}
	}
	
	modLoaded["frndfav"].timerId = setInterval("checkModuleTimeout('frndfav')","60000");
	for(var i=0; i<friendData.length; i++){
		var f = friendData[i];
		if((typeof f.favMusic != 'undefined') && f.favMusic.length > 0){
			friendFavMusicFound = "true";
			for(var j=0; j<f.favMusic.length && j<4; j++){
				getArtistInfo(f.favMusic[j]);
			}
		}
	}
	
	/*
		show a message if none of the users friends has any music 
		interest defined in their profile
	*/
	if(friendFavMusicFound == "false"){
		var frndfavout = "";
		frndfavout += '<div id="friendfavs">';
		frndfavout += '<h1 style="color:#007CD3">Friend Music Updates</h1>';
		frndfavout += '<div style="width:100%;height:752px;overflow:auto">';
		frndfavout += '<p>Your friends dont like music</p>';		
		frndfavout += '</div>';
		frndfavout += '</div>';
		
		document.getElementById('friendFavContainer').innerHTML = frndfavout;
		//setModuleLoaded();
		modLoaded["frndfav"].loaded = "true";
	}
}

function getArtistInfo(artist, isLastArtist){
	var found = "false";		
	
	for(var i=0; i<artistData.length; i++){
		var artst = artistData[i];
		if(artst.artist == artist){
			found = "true";
		} 
	}
	
	if(found == "false"){
		var url = "http://api.hosted.thumbplay.com/api/rest/search/content?artist="+encodeURI(artist)+"&pname=1456";
		var tpParams = {};
        	tpParams[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.DOM;
        gadgets.io.makeRequest(url, getArtistInfoHandler, tpParams);
        reqCount++;
	}
}

function getArtistInfoHandler(obj){
	if(typeof obj != 'undefined' && typeof obj.data != 'undefined'){
		var data = obj.data;
		var errors = data.getElementsByTagName('errors');
		
		if(errors.length == 0){
			var contentNodes = data.getElementsByTagName('contentResult');
			
			if(contentNodes.length > 0){
				var info = new Object();
				var cn = contentNodes.item(0).childNodes;
				
				for(var j=0; j<cn.length; j++){
					currNode = cn.item(j); 
					n = currNode.nodeName;
					switch(n){
						case "title":
							info.title = currNode.firstChild.nodeValue;
							break;
						case "titleLandingPageURL":
							info.titleLanding = currNode.firstChild.nodeValue;
							break;
						case "artist":
							info.artist = currNode.firstChild.nodeValue;
							break;
						case "artistLandingPageURL":
							info.artistLanding = currNode.firstChild.nodeValue;
							break;
						case "imageSmallURL":
							info.image = currNode.firstChild.nodeValue;
							break;
						case "previewURL":
							info.preview = currNode.firstChild.nodeValue;
							break;
						case "contentId":
							info.contentId = currNode.firstChild.nodeValue;
							break;
					}
				}
				
				artistData.push(info);
			} else {
				var info = new Object();
				info.error = "Not Found";
				artistData.push(info);
			}
		} else {
			var info = new Object();
			info.error = "Not Found";
			artistData.push(info);
		}
	} else {
		var info = new Object();
		info.error = "Not Found";
		artistData.push(info);
	}
	
	if(artistCount == artistData.length){
		createFavFrndView("false");
		artistCount = 0;
	}
}

function createFavFrndView(showErr){
	var err = showErr;
	
	for(var i=0; i<friendData.length; i++){
		var ffview = "";
		var f = friendData[i];
		if((typeof f.favMusic != "undefined") && f.favMusic.length > 0){
			for(var j=0; j<f.favMusic.length && j<4; j++){
				for(var k=0; k<artistData.length; k++){
					if(typeof artistData[k].error == 'undefined'){
						var ar = artistData[k];
						if(ar.artist == f.favMusic[j]){
							ffview += '<div class="favContent">';
							ffview += '<h2><a href="http://www.hi5.com/friend/profile/displayProfile.do?userid='+f.friendId+'">'+f.friendName+'</a> likes</h2>';
							ffview += '<div class="favDetails">';
							ffview += "<img src=\""+f.thumbnail+"\" style=\"float:left;margin:0 10px 0 0;\"/>";
							ffview += '<div style="float:left;width:120px;">';
							ffview += "<a href=\"#\" onclick=\"showPostInstallFrame('"+ar.contentId+"')\">"+ar.title+"</a>";
							ffview += "<a href=\"#\"><img src=\""+base_url+"/images/play_btn.gif\" class=\"stopped\" onclick=\"createFlashEmbed('"+ar.preview+"',this,'canvas');return false;\" style=\"border:none;margin-left:5px;position:relative;top:3px;\"/></a><br/>";
							ffview += "<a href=\"#\" style=\"font-weight:bold;\" onclick=\"showPostInstallFrame('"+ar.contentId+"')\">"+ar.artist+"</a>";								
							ffview += '<p style="color:#666666;margin:3px 0;font-size:11px">Music Ringtone</p>';								
							ffview += '</div>';
							ffview += '</div>';
							
							ffview += '</div>';
							
							frndViewList.push(ffview);
							
							musOut = "";
							ffview = "";
						}
					}
				}
			}
		} else {
			ffview += '<div class="favContent">';
			ffview += '<h2><a href="http://www.hi5.com/friend/profile/displayProfile.do?userid='+f.friendId+'">'+f.friendName+'</a> likes</h2>';
			ffview += "<p style='float:left'>Your friend doesn't like music</p>";
			ffview += '</div>';
			
			frndViewList.push(ffview);
			ffview = "";
		}
	}
	
	if(err == "true"){
		showFrndFavList("true");
	} else {
		showFrndFavList("false");
	}
}

function showFrndFavList(showError){
	var frndfavout = "";
	var viewItemlimit = 5;
	var tempCount = 0;
	var err = showError;
	
	frndfavout += '<div id="friendfavs">';
	frndfavout += '<h1 style="color:#007CD3">Friend Music Updates</h1>';
	frndfavout += '<div style="width:100%;height:752px;overflow:auto">';
	if(err != "true"){
		if(frndViewIndx == frndViewList.length){
			frndViewIndx = 0;
		}
		
		for(var i=frndViewIndx; i<frndViewList.length && (i<(frndViewIndx + viewItemlimit)); i++){
			tempCount = i;
			frndfavout += frndViewList[i];
		}	
		
		frndViewIndx = tempCount + 1;
		
		if(frndViewList.length > viewItemlimit){
			document.getElementById('frndvm').style.visibility = 'visible';
		} else {
			document.getElementById('frndvm').style.visibility = 'hidden';
		}
	} else {
		frndfavout += '<p style="font-weight:bold">There was and error loading, please try again later</p>'
	}
	frndfavout += '</div>';
	frndfavout += '</div>';
	
	//document.getElementById('gallery_module').style.display = "";
	document.getElementById('friendFavContainer').innerHTML = frndfavout;
	//setModuleLoaded();
	modLoaded["frndfav"].loaded = "true";
}

function getTopArtists(){
	var url = "http://api2.thumbplay.com/api/rest/catalog/artistItem?moduleCode=about&pname=1465";
	var tpParams = {};
       	tpParams[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.DOM;
    modLoaded["tpa"].timerId = setInterval("checkModuleTimeout('tpa')","60000");
    gadgets.io.makeRequest(url, getTopArtistHandler, tpParams);
}

function getTopArtistHandler(obj) {
	if(typeof obj != 'undefined' && typeof obj.data != 'undefined'){
		var data = obj.data;
		var errors = data.getElementsByTagName('errors');
		
		if(errors.length == 0){
			var contentNodes = data.getElementsByTagName('name');
			
			if(contentNodes.length > 0){
				for(var i=0; i<contentNodes.length; i++){
					topArtists.push(contentNodes[i].firstChild.nodeValue);
				}
			}
			
			showTopArtists("false");
		} else {
			document.getElementById('leftContainer').innerHTML = '<p>There was an error getting top artists</p>';
			setModuleLoaded();
		}
	}
}

function showTopArtists(showError){
	var tpa = '<h1 style="color:#007CD3">Top Artists</h1>';
	var err = showError;
	
	if(err != "true"){
		tpa += "<ol>";
		for(var i=0; i<topArtists.length; i++){
			tpa += '<li><a href="http://ringtones.thumbplay.com/'+encodeURI(topArtists[i].replace(new RegExp(" ","g"),"+"))+'-ringtones" target="_blank">'+topArtists[i]+'</a></li>';
		}
		tpa += "</ol>";
	} else {
		tpa += '<p style="font-weight:bold">There was an error loading the top artists</p>';
	}
	
	document.getElementById('tas').innerHTML = tpa;
	//setModuleLoaded();
	modLoaded["tpa"].loaded = "true";
}

function getTopDls(){
	var url = "http://api.hosted.thumbplay.com/api/rest/catalog/contentItem?moduleCode=ringtone-chart&pname=1456";
	var tpParams = {};
       	tpParams[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.DOM;
    modLoaded["tdl"].timerId = setInterval("checkModuleTimeout('tdl')","60000");
    gadgets.io.makeRequest(url, getTopDlHandler, tpParams);
}

function getTopDlHandler(obj){
	if(typeof obj != 'undefined' && typeof obj.data != 'undefined'){
		var data = obj.data;
		var errors = data.getElementsByTagName('errors');
		var linksurl = "http://api.hosted.thumbplay.com/api/rest/offers/partnerLinks?contentIds=";
		
		if(errors.length == 0){
			var contentNodes = data.getElementsByTagName('contentFullInfo');
			
			if(contentNodes.length > 0){
				for(var i=0; i<contentNodes.length; i++){
					var cfchnode = contentNodes[i].childNodes;
					var dlContent = new Object();
					for(var k=0; k<cfchnode.length; k++){
						if(cfchnode[k].nodeName == 'artistShortInfo'){
							var arInfo = cfchnode[k].childNodes;
							for(var j=0; j<arInfo.length; j++){
								if(arInfo[j].nodeName == 'name'){
									dlContent.artist = arInfo[j].firstChild.nodeValue; 
								}
							}
						} else if(cfchnode[k].nodeName == 'name'){
							dlContent.name = cfchnode[k].firstChild.nodeValue; 
						}  else if(cfchnode[k].nodeName == 'contentId'){
						 	var idInfo = cfchnode[k].childNodes;
						 	for(var j=0; j<idInfo.length; j++){
								if(idInfo[j].nodeName == 'id'){
									dlContent.contentId = idInfo[j].firstChild.nodeValue; 
								}
							}
						}
					}
					topDownloads.push(dlContent);
				}
				
				for(var i=0; i<topDownloads.length; i++){
					if(i != topDownloads.length-1){
						linksurl += topDownloads[i].contentId + ",";
					} else {
						linksurl += topDownloads[i].contentId;
					}
				}
				linksurl += "&pname=1456&plpFlow=atb4";
				var tpParams = {};
       			tpParams[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.DOM;
    			gadgets.io.makeRequest(linksurl, function(obj){
    				if(typeof obj.data != 'undefined'){
    					parseLinksData(topDownloads, obj.data);
    					showTopDls("false");
    				} else {
    					showTopDls("true");
    				}
    			}, tpParams);
			}
		}
	}
}

function showTopDls(showError){
	var err = showError;
	var topDl = '<h1 style="color:#007CD3">Top Downloads</h1>';
	
	if(err != "true"){
		topDl += "<ol>";
		for(var i=0; i<topDownloads.length; i++){
			topDl += '<li><a href="#" onclick="showPostInstallFrame('+topDownloads[i].contentId+')">'+topDownloads[i].name+'</a></li>';	
		}
		topDl += "</ol>";
	} else {
		topDl += '<p style="font-weight:bold">There was an error loading the top downloads</p>';
	}
	//document.getElementById('gallery_module').style.display = '';
	document.getElementById('tdl').innerHTML = topDl;
	//setModuleLoaded();
	modLoaded["tdl"].loaded = "true";	
}

function getNewTones(){
	var url = "http://api2.thumbplay.com/api/rest/catalog/contentItem?moduleCode=new-realtone&pname=1456";
	var tpParams = {};
       	tpParams[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.DOM;
    modLoaded["nrtones"].timerId = setInterval("checkModuleTimeout('nrtones')","60000");
    gadgets.io.makeRequest(url, getNewTonesHandler, tpParams);	
}

function getNewTonesHandler(obj){
	if(typeof obj != 'undefined' && typeof obj.data != 'undefined'){
		var data = obj.data;
		var errors = data.getElementsByTagName('errors');
		var linksurl = "http://api.hosted.thumbplay.com/api/rest/offers/partnerLinks?contentIds=";
		
		if(errors.length == 0){
			var contentNodes = data.getElementsByTagName('contentFullInfo');
			
			if(contentNodes.length > 0){
				for(var i=0; i<contentNodes.length; i++){
					var cfchnode = contentNodes[i].childNodes;
					var ntone = new Object();
					for(var k=0; k<cfchnode.length; k++){
						if(cfchnode[k].nodeName == 'artistShortInfo'){
							var arInfo = cfchnode[k].childNodes;
							for(var j=0; j<arInfo.length; j++){
								if(arInfo[j].nodeName == 'name'){
									ntone.artist = arInfo[j].firstChild.nodeValue; 
								}
							}
						} else if(cfchnode[k].nodeName == 'name'){
							ntone.name = cfchnode[k].firstChild.nodeValue; 
						} else if(cfchnode[k].nodeName == 'previewPath') {
							ntone.preview = cfchnode[k].firstChild.nodeValue;
						} else if(cfchnode[k].nodeName == 'imageSmall'){
							ntone.albumart = cfchnode[k].firstChild.nodeValue;
						} else if(cfchnode[k].nodeName == 'contentId'){
						 	var idInfo = cfchnode[k].childNodes;
						 	for(var j=0; j<idInfo.length; j++){
								if(idInfo[j].nodeName == 'id'){
									ntone.contentId = idInfo[j].firstChild.nodeValue; 
								}
							}
						}
					}
					newTones.push(ntone);
				}
				
				for(var i=0; i<newTones.length; i++){
					if(i != newTones.length-1){
						linksurl += newTones[i].contentId + ",";
					} else {
						linksurl += newTones[i].contentId;
					}
				}
				linksurl += "&pname=1456&plpFlow=atb4";
				var tpParams = {};
       			tpParams[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.DOM;
    			gadgets.io.makeRequest(linksurl, function(obj){
    				if(typeof obj.data != 'undefined'){
    					parseLinksData(newTones, obj.data);
        				showNewTones("false");
    				} else {
    					showNewTones("true");
    				}
    			}, tpParams);
    			
    			//showNewTones("false");
			}
		}
	}
}

function showNewTones(showError){
	var newtonesout = "";
	var viewItemlimit = 6;
	var tempCount = 0;
	var err = showError;
	
	if(err != "true"){
		if(newToneViewIndx == newTones.length){
			newToneViewIndx = 0;
		}
		
		/*if(newToneViewIndx > 0){
			newToneViewIndx++;
		}*/
		
		newtonesout += '<div class="row">';
		for(var i=newToneViewIndx; i<newTones.length && (i<(newToneViewIndx+viewItemlimit)); i++){
			tempCount = i;
			if(tempCount%2 == 0){
				newtonesout += '<div class="leftcol">';
			} else {
				newtonesout += '<div class="rightcol">';
			}
			newtonesout +=  '<div class="ringtoneInfo">';
			newtonesout += '<a href="#" onclick="showPostInstallFrame('+newTones[i].contentId+')"><img src="'+newTones[i].albumart+'" style="float:left;margin:0 10px 0 0;border:none;"/></a>';
			newtonesout += '<div style="float:left;width:172px">';
			newtonesout += '<a href="#" onclick="showPostInstallFrame('+newTones[i].contentId+')">'+newTones[i].name+'</a><br/>';				
			newtonesout += '<a href="#" style="font-weight:bold;" onclick="showPostInstallFrame('+newTones[i].contentId+')">'+newTones[i].artist+'</a><br/>';
			newtonesout += "<a href=\"#\"><img src=\""+base_url+"/images/play_btn.gif\" style=\"border:none;position:relative;top:3px\" class=\"stopped\" onclick=\"createFlashEmbed('"+newTones[i].preview+"',this,false);return false;\"/></a>"; 		
			//newtonesout += '<span style="color:#666666">music ringtone</span><a href="'+newTones[i].purchaseUrl+'"><img src="'+base_url+'/images/getit_btn.gif" style="border:none;padding-left:5px;position:relative;top:6px"/></a>';
			newtonesout += "<span style=\"color:#666666\">music ringtone</span><a href=\"#\")\"><img src=\""+base_url+"/images/getit_btn.gif\" style=\"border:none;padding-left:5px;position:relative;top:6px\" onclick=\"showPostInstallFrame('"+newTones[i].contentId+"')\"/></a>";
			
			newtonesout += "</div>";	
			newtonesout += "</div>";	
			newtonesout += "</div>";
			if(tempCount%2 != 0){
				newtonesout += '</div>';
				if(tempCount != ((newToneViewIndx+viewItemlimit)-1)){
					newtonesout += '<div class="row">';
				}
			}
		}
		//newtonesout += '</div>';
		newToneViewIndx = tempCount + 1;
		
		if(newTones.length > 6){
			document.getElementById('newtonevm').style.visibility = 'visible';
		} else {
			document.getElementById('newtonevm').style.visibility = 'hidden';
		}
	} else {
		newtonesout += '<p style="font-weight:bold">There was an error loading new ringtones</p>';
	}
	
	//document.getElementById('gallery_module').style.display = '';
	document.getElementById('newringtones').innerHTML = newtonesout;
	//setModuleLoaded();
	modLoaded["nrtones"].loaded = "true";
}

function getJustDled(){
	var url = "http://api2.thumbplay.com/api/rest/content/justDownloaded?limit=10&pname=1456";
	var tpParams = {};
       	tpParams[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.DOM;
    modLoaded["jdled"].timerId = setInterval("checkModuleTimeout('jdled')","60000");   	
    gadgets.io.makeRequest(url, getJustDledHandler, tpParams);	
}

function getJustDledHandler(obj){
	if(typeof obj != 'undefined' && typeof obj.data != 'undefined'){
		var data = obj.data;
		var errors = data.getElementsByTagName('errors');
		var linksurl = "http://api.hosted.thumbplay.com/api/rest/offers/partnerLinks?contentIds=";
		
		if(errors.length == 0){
			var contentNodes = data.getElementsByTagName('content');
			
			if(contentNodes.length > 0){
				for(var i=0; i<contentNodes.length; i++){
					var cfchnode = contentNodes[i].childNodes;
					var dltone = new Object();
					for(var k=0; k<cfchnode.length; k++){
						if(cfchnode[k].nodeName == 'name'){
							dltone.name = cfchnode[k].firstChild.nodeValue; 
						} else if(cfchnode[k].nodeName == 'previewPath') {
							dltone.preview = cfchnode[k].firstChild.nodeValue;
						} else if(cfchnode[k].nodeName == 'imageSmall'){
							dltone.albumart = cfchnode[k].firstChild.nodeValue;
						} else if(cfchnode[k].nodeName == 'contentId'){
						 	var idInfo = cfchnode[k].childNodes;
						 	for(var j=0; j<idInfo.length; j++){
								if(idInfo[j].nodeName == 'id'){
									dltone.contentId = idInfo[j].firstChild.nodeValue; 
								}
							}
						} else if(cfchnode[k].nodeName == 'contentType'){
							var contentType = cfchnode[k].childNodes;
							for(var j=0; j<contentType.length; j++){
								if(contentType[j].nodeName == 'name'){
									 dltone.contentType = contentType[j].firstChild.nodeValue;
								}							
							}
						}
					}
					
					if(dltone.contentType == 'Music ringtone'){
						justDled.push(dltone);
					}
				}
				
				for(var i=0; i<justDled.length; i++){
					if(i != justDled.length-1){
						linksurl += justDled[i].contentId + ",";
					} else {
						linksurl += justDled[i].contentId;
					}
				}
				linksurl += "&pname=1456&plpFlow=atb4";
				var tpParams = {};
       			tpParams[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.DOM;
    			gadgets.io.makeRequest(linksurl, function(obj){
    				if(typeof obj.data != 'undefined'){
    					parseLinksData(justDled, obj.data);
    					showjustDled("false");
    				} else {
    					showjustDled("true");
    				}
    			}, tpParams);
    			
    			//showjustDled("false");	
			}
		}
	}
}

function showjustDled(showError){
	var jdl = "";
	var viewItemlimit = 6;
	var tempCount = 0;
	var err = showError;
	
	if(err != "true"){
		if(dlViewIndx == justDled.length){
			dlViewIndx = 0;
		}
		
		jdl += '<div class="row">';
		for(var i=dlViewIndx; i<justDled.length && (i<(dlViewIndx+viewItemlimit)); i++){
			tempCount = i;
			if(tempCount%2 == 0){
				jdl += '<div class="leftcol">';
			} else {
				jdl += '<div class="rightcol">';
			}
			jdl +=  '<div class="ringtoneInfo">';
			jdl += '<a href="#" onclick="showPostInstallFrame('+justDled[i].contentId+')"><img src="'+justDled[i].albumart+'" style="float:left;margin:0 10px 0 0;border:none"/></a>';
			jdl += '<div style="float:left;width:172px">';
			jdl += '<a href="#" onclick="showPostInstallFrame('+justDled[i].contentId+')">'+justDled[i].name+'</a><br/>';				
			jdl += '<a href="#" style="font-weight:bold;" onclick="showPostInstallFrame('+justDled[i].contentId+')">'+justDled[i].artist+'</a><br/>';
			//jdl += '<a href="'+justDled[i].preview+'" target="_blank"><img src="http://ostestapp.googlecode.com/svn/trunk/images/play_btn.gif" style="border:none"/></a>'; 		
			jdl += "<a href=\"#\"><img src=\""+base_url+"/images/play_btn.gif\" style=\"border:none;position:relative;top:3px\" class=\"stopped\" onclick=\"createFlashEmbed('"+justDled[i].preview+"',this,'canvas');return false;\"/></a><br/>"; 		
			//jdl += '<span style="color:#666666">music ringtone</span><a href="'+justDled[i].purchaseUrl+'"><img src="'+base_url+'/images/getit_btn.gif" style="border:none;padding-left:5px;position:relative;top:6px"/></a>';
			jdl += "<span style=\"color:#666666\">music ringtone</span><a href=\"#\"><img src=\""+base_url+"/images/getit_btn.gif\" style=\"border:none;padding-left:5px;position:relative;top:6px\" onclick=\"showPostInstallFrame('"+justDled[i].contentId+"')\"/></a>";
			
			jdl += "</div>";	
			jdl += "</div>";	
			jdl += "</div>";
			if(tempCount%2 != 0){
				jdl += '</div>';
				if(tempCount != ((dlViewIndx + viewItemlimit)-1)){
					jdl += '<div class="row">';
				}
			}	
		}
		
		dlViewIndx = tempCount + 1;
		
		if(justDled.length > viewItemlimit){
			document.getElementById('jdlvm').style.visibility = 'visible';
		} else {
			document.getElementById('jdlvm').style.visibility = 'hidden';
		}
	} else {
		jdl += '<p style="font-weight:bold;">Sorry there was an error loading just downloaded contents</p>';
	}
	
	//document.getElementById('gallery_module').style.display = '';
	document.getElementById('justdled').innerHTML = jdl;
	//setModuleLoaded();
	modLoaded["jdled"].loaded = "true";
}

function parseLinksData(arrayObj,data){
	if(typeof data != 'undefined'){
		var errors = data.getElementsByTagName('errors');
		
		if(errors.length == 0){
			var contentNodes = data.getElementsByTagName('offerLinks');
			
			if(contentNodes.length > 0){
				for(var i=0; i<contentNodes.length; i++){
					var olChilds = contentNodes[i].childNodes;
					var contentId;
					var artist;
					var artistUrl;
					var purchaseUrl;
					var titleLanding;
					for(var j=0; j<olChilds.length; j++){
						if(olChilds[j].nodeName == 'contentId'){
							contentId = olChilds[j].firstChild.nodeValue; 
						} else if(olChilds[j].nodeName == 'artistURL'){
							artist = olChilds[j].firstChild.nodeValue.toString().replace("http://ringtones.thumbplay.com/","");
							artist = artist.replace("-ringtones","");
							artist = artist.replace(new RegExp(escapeRegexp("+"),"g")," ");
							artist = artist.replace(new RegExp("%[0-9]*","g")," ");
						} else if(olChilds[j].nodeName == 'webPurchaseURL'){
							purchaseUrl = olChilds[j].firstChild.nodeValue;
						} else if(olChilds[j].nodeName == 'titleLandingPageURL'){
							titleLanding = olChilds[j].firstChild.nodeValue;
						} else if(olChilds[j].nodeName == 'artistLandingPageURL'){
							artistUrl = olChilds[j].firstChild.nodeValue;
						}
					}
					
					for(var k=0; k<arrayObj.length; k++){
						if(arrayObj[k].contentId == contentId){
							arrayObj[k].artist = artist;
							arrayObj[k].artistUrl = artistUrl;									
							arrayObj[k].purchaseUrl = purchaseUrl;									
							arrayObj[k].titleLanding = titleLanding;									
						}
					}
				}
			}
		}
	}
}

function escapeRegexp(str)
{
  var specials = new RegExp("[.*+?|()\\[\\]{}\\\\]", "g"); // .*+?|()[]{}\
  return str.replace(specials, "\\$&");
}

function setModuleLoaded(){
	gModToLoad--;
}

function checkAllMudulesLoaded(){
	var allModulesLoaded = "true";
	/*if(gModToLoad == 0){
		document.getElementById('loading').style.display = "none";
		document.getElementById('gallery_module').style.display = "";
		clearInterval(modLoadTimer);
		
		//load some test ringtone purchase data and create an activity feed
		globalPurchaseData = new Array();
		loadTestData();
		hasNewRingtone = "true";
		
		for(var i=0; i<globalPurchaseData.length; i++){
			getRingtoneInfo(globalPurchaseData[i]);
		}
	}*/
	
	for(var i=0; i<gallery_modules.length; i++){
		var obj = modLoaded[gallery_modules[i]];
		if(obj.loaded == "false"){
			allModulesLoaded = "false";
		}
	}
	
	if(allModulesLoaded == "true"){
		document.getElementById('loading').style.display = "none";
		document.getElementById('gallery_module').style.display = "";
		clearInterval(modLoadTimer);
		
		gadgets.window.adjustHeight();
		
		//load some test ringtone purchase data and create an activity feed
		/*globalPurchaseData = new Array();
		loadTestData();
		hasNewRingtone = "true";
		
		for(var i=0; i<globalPurchaseData.length; i++){
			getContentInfo(globalPurchaseData[i]);
		}*/	
	}
}

function checkModuleTimeout(moduleName) {
	switch(moduleName){
		case "tpa": 
			if(modLoaded["tpa"].loaded == "false"){
				showTopArtists("true");				
			}
			clearInterval(modLoaded["tpa"].timerId);
			break;
		case "tdl": 
			if(modLoaded["tdl"].loaded == "false"){
				showTopDls("true");				
			}
			clearInterval(modLoaded["tdl"].timerId);
			break;
		case "nrtones": 
			if(modLoaded["nrtones"].loaded == "false"){
				showNewTones("true");				
			}
			clearInterval(modLoaded["nrtones"].timerId);
			break;
		case "jdled": 
			if(modLoaded["jdled"].loaded == "false"){
				showjustDled("true");				
			}
			clearInterval(modLoaded["jdled"].timerId);
			break;
		case "frndfav": 
			if(modLoaded["frndfav"].loaded == "false"){
				createFavFrndView("true");				
			}
			clearInterval(modLoaded["frndfav"].timerId);
			break;
	}
}